using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class DMHSBOCollection : Csla.BusinessListBase<DMHSBOCollection, DMHSBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            DMHSBO _DMHSBO = DMHSBO.NewChildDMHSBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_DMHSBO));
            Add(_DMHSBO);
            return _DMHSBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private DMHSBOCollection()
        {
        }

        public static DMHSBOCollection NewDMHSBOCollection()
        {
            return new DMHSBOCollection();
        }

        public static DMHSBO NewDMHSBO()
        {
            return DMHSBO.NewChildDMHSBO();
        }

        public static DMHSBOCollection GetDMHSBOCollection()
        {
            SPParam spParam = new SPParam("DMID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("DMID", "ASC", "");
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(string where)
        {
            SPParam spParam = new SPParam("DMID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("DMID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DMID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DMID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DMID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }

        public static DMHSBOCollection GetDMHSBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DMID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DMHSBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DMHSBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    DMHSBO objDMHSBO = DMHSBO.GetChildDMHSBO(dr);
                    objDMHSBO.STT0 = ++stt;
                    this.Add(objDMHSBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            foreach (DMHSBO child in this)
            {
                if (child.IsNew)
                    child.Insert();
                else
                    child.Update();
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public DMHSBO GetDMHSBO(int _DMID)
        {
            DMHSBO returnObject = null;
            foreach (DMHSBO _DMHSBO in this)
            {
                if (_DMHSBO.DMID == _DMID)
                {
                    returnObject = _DMHSBO;
                    break;
                }
            }
            return returnObject;
        }

        public void Remove(int DMID)
        {
            foreach (DMHSBO _DMHSBO in this)
            {
                if (_DMHSBO.DMID == DMID)
                {
                    try
                    {
                        this.Remove(_DMHSBO);
                    }
                    catch { }
                    break;
                }
            }
        }

        public void SortByStt()
        {
            SortByStt(0, this.Count - 1);
        }

        public void SortByStt(int beg, int end)
        {
            if (end <= beg)
                return;
            else
            {
                int pivot = GetPivotPointByStt(beg, end);
                if (pivot > beg)
                    SortByStt(beg, pivot - 1);
                if (pivot < end)
                    SortByStt(pivot + 1, end);
            }
        }

        private int GetPivotPointByStt(int begPoint, int endPoint)
        {
            int pivot = begPoint;
            int m = begPoint + 1;
            int n = endPoint;
            int sttPivot = StaticUtil.GetInt(this[pivot].SOTT, int.MaxValue);
            int sttM = StaticUtil.GetInt(this[m].SOTT, int.MaxValue);
            int sttN = StaticUtil.GetInt(this[n].SOTT, int.MaxValue);
            DMHSBO temp = null;

            while ((m < endPoint) && sttPivot.CompareTo(sttM) >= 0)
            {
                m++;
                sttM = StaticUtil.GetInt(this[m].SOTT, int.MaxValue);
            }

            while ((n > begPoint) && sttPivot.CompareTo(sttN) <= 0)
            {
                n--;
                sttN = StaticUtil.GetInt(this[n].SOTT, int.MaxValue);
            }

            while (m < n)
            {
                temp = this[m].Clone();
                this[m] = this[n].Clone();
                this[n] = temp;
                sttM = StaticUtil.GetInt(this[m].SOTT, int.MaxValue);
                sttN = StaticUtil.GetInt(this[n].SOTT, int.MaxValue);

                while (m < endPoint && sttPivot.CompareTo(sttM) >= 0)
                {
                    m++;
                    sttM = StaticUtil.GetInt(this[m].SOTT, int.MaxValue);
                }

                while (n > begPoint && sttPivot.CompareTo(sttN) <= 0)
                {
                    n--;
                    sttN = StaticUtil.GetInt(this[n].SOTT, int.MaxValue);
                }
            }

            if (pivot != n)
            {
                temp = this[n].Clone();
                this[n] = this[pivot].Clone();
                this[pivot] = temp;
            }

            return n;
        }
        #endregion Business Method
        //==========================================================================
    }
}
